package com.sunseagear.wind.modules.sys.json;

import com.sunseagear.common.base.http.R;
import com.sunseagear.common.oss.exception.FileNameLengthLimitExceededException;
import com.sunseagear.common.oss.exception.InvalidExtensionException;
import com.sunseagear.common.utils.FileUtils;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.helper.AttachmentHelper;
import com.sunseagear.wind.modules.oss.service.IAttachmentService;
import com.sunseagear.wind.common.utils.BASE64DecodedMultipartFile;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.fileupload.FileUploadBase;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

/**
 *
 * @version V1.0
 * @package com.sunseagear.oss.controller
 * @title: 附件管理控制器
 * @description: 附件管理控制器 * @date: 2018-04-25 14:25:55
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */

@RestController
@RequestMapping("json/oss")
@RequiredArgsConstructor
public class OSSUploadJsonController {

    private final IAttachmentService attachmentService;
    private final AttachmentHelper attachmentHelper;

    /**
     * @title: ajaxUpload
     * @description: 文件上传
     * @return: AjaxUploadResponse
     */
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public R<Object> upload(HttpServletRequest request, MultipartFile[] file, @RequestParam(required = false, defaultValue = "") String dir) {
        try {
            return R.success((Object) attachmentHelper.upload(request, file, dir));
        } catch (IOException | FileNameLengthLimitExceededException | FileUploadBase.FileSizeLimitExceededException |
                 InvalidExtensionException e) {
            return R.fail("上传失败");
        }
    }

    @RequestMapping("imgToBase64")
    public R<Object> uploadImg(MultipartFile file) {

        String fileType = file.getOriginalFilename();
        fileType = Objects.requireNonNull(FileUtils.getFileType(fileType)).toLowerCase();
        String base64 = FileUtils.fileToBase64(file);
        if (base64 == null) {
            R.fail("转换失败");
        }
        base64 = String.format("data:image/%s;base64,%s", fileType, base64);
        return R.success((Object) base64);

    }

    @PostMapping("uploadBase")
    public R<Object> uploadImg(HttpServletRequest request, String content, @RequestParam(required = false, defaultValue = "") String dir) {
        if (!StringUtils.isEmpty(content)) {
            MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(content);
            try {
                MultipartFile[] array = {multipartFile};
                return R.success((Object) attachmentHelper.upload(request, array, dir));
            } catch (IOException | FileNameLengthLimitExceededException |
                     FileUploadBase.FileSizeLimitExceededException | InvalidExtensionException e) {
                return R.fail("上传失败");
            }
        } else {
            return R.fail("图片不能为空");
        }

    }
}
